<?php
include_once 'includes/check_user.php';
include_once dirname(__FILE__) . '/mysql_connect.php';
include_once dirname(__FILE__) . '/class.mydict.php';
$answer = '';
$key = '';
$meaning = '';
$date_query = 'all';
$mode = 'rotating';
$row = null;
if (isset($_REQUEST['next'])) {
	$date_query = $_REQUEST['date_query'];
	$mode = $_REQUEST['mode'];
	$row = get_next_word($date_query, $mode);
	if ($row) {
		$key = $row['key'];
		$meaning = $row['meaning'];
	}
}
if (isset($_POST['check'])) {
	$key = $_POST['key'];
	$answer = $_POST['answer'];
} elseif ($key == '') {
	$row = get_next_word($date_query, $mode);
	if ($row) {
		$key = $row['key'];
		$meaning = $row['meaning'];
	}
}
?>
<form name="mode" method="get">	
	<fieldset><legend>Word selection</legend>
	Date: 
	<select name="date_query">
		<option<?php echo ($date_query == 'all' ? ' selected' : '')?> value="all">All</option>
		<option<?php echo ($date_query == 'today' ? ' selected' : '')?> value="today">Today</option>
		<option<?php echo ($date_query == 'this_week' ? ' selected' : '')?> value="this_week">This week</option>
		<option<?php echo ($date_query == 'this_month' ? ' selected' : '')?> value="this_month">This month</option>
		<option<?php echo ($date_query == 'this_year' ? ' selected' : '')?> value="this_year">This year</option>
	</select>
	Mode: 
	<input name="mode" type="radio" value="rotating"<?php echo ($mode == 'rotating' ? ' checked' : '')?>>Rotating</input>
	<input name="mode" type="radio" value="hard_word"<?php echo ($mode == 'hard_word' ? ' checked' : '')?>>Hard word</input>
	<input type="submit" name="next" value="Next" />
	</fieldset>
</form>
<?php if (!isset($_POST['check'])) : ?>
	<?php if ($row) : ?>
			<form name="check" method="post">
				<fieldset>
				<legend>Current word</legend>
				meaning: <?php echo $row['meaning']?> | review: <?php echo $row['review']?> | correct: <?php echo $row['check_correct']?> | incorrect: <?php echo $row['check_incorrect']?>
				<input type="hidden" name="key" value="<?php echo $key?>" />
				<input id="answer" name="answer" type="text" value="<?php echo $answer?>" size="80" placeholder="Enter key to check in Dict" />
				<input name="check" type="submit" value="Check" />
				</fieldset>
			</form>
			<hr />
	<?php endif;
else :
	echo '<hr />';
	$row = MyDict::queryKey($key);
	echo '<a href="index.php?search=Search&key=' . $answer . '">' . $answer . '</a>';
	if ($row['key'] == $answer) {
		echo ' is correct!<br />';
		MyDict::checkCorrect($row['id']);
	} else {
		echo ' is incorrect!<br />';
		MyDict::checkIncorrect($row['id']);
	}
	echo 'Word: <a href="index.php?search=Search&key=' . $row['key'] . '">' . $row['key'] . '</a><br />';
	echo $row['note'];
endif;
function get_next_word($date_query, $mode) {
	// reset session cache
	if (!(isset($_SESSION['date_query']) && isset($_SESSION['mode']) && isset($_SESSION['review_words']) && isset($_SESSION['review_index']))
	|| ($date_query != $_SESSION['date_query']) || ($mode != $_SESSION['mode'])) {
		$_SESSION['date_query'] = $date_query;
		$_SESSION['mode'] = $mode;
		$_SESSION['review_index'] = 0;
		$_SESSION['review_words'] = MyDict::getReviewWords($date_query, $mode);
	}
	if (empty($_SESSION['review_words'])) return null;
	$row = $_SESSION['review_words'][$_SESSION['review_index']];
	$_SESSION['review_index'] = (intval($_SESSION['review_index']) + 1) % sizeof($_SESSION['review_words']);
	return $row;
}
?>